package com.infore.dataReceiving.core.filter.instructions;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import org.springframework.util.StringUtils;
import com.infore.dataReceiving.core.chain.Water2018InstructionsChain;
import com.infore.dataReceiving.core.enums.Water2018InstructionsTypeEnum;
import com.infore.dataReceiving.core.filter.Water2018InstructionsFilter;
import com.infore.dataReceiving.dto.WaterInstructionsDto;
import com.infore.dataReceiving.entity.EquipmentInfo;
import com.infore.dataReceiving.repository.IBackControlLogRepository;
import com.infore.dataReceiving.util.CRCCheck;
import com.infore.dataReceiving.util.Water2018InstructionsHandleUtil;
import com.infore.dataReceiving.util.RedisUtil;
import io.netty.channel.ChannelHandlerContext;

/**
 * 设备信息（日志，状态，参数）
 * @author 69457
 *
 */
public class EquipmentInfoFilter implements Water2018InstructionsFilter{
	
	public static final Map<String, Object> equipmentMap = new HashMap<>();
	
	static {
		equipmentMap.put("i11001", "分析仪(日志)-运行日志");
		equipmentMap.put("i21001", "工控机(日志)-运行日志");
		Map<String, String> map = new HashMap<String, String>();//分析仪工作状态
		map.put("0", "分析仪(状态)-工作状态-空闲");
		map.put("1", "分析仪(状态)-工作状态-水样测试");
		map.put("2", "分析仪(状态)-工作状态-标样核查");
		map.put("3", "分析仪(状态)-工作状态-零点核查");
		map.put("4", "分析仪(状态)-工作状态-跨度核查");
		map.put("5", "分析仪(状态)-工作状态-空白测试");
		map.put("6", "分析仪(状态)-工作状态-平行样测试");
		map.put("7", "分析仪(状态)-工作状态-加标回收");
		map.put("8", "分析仪(状态)-工作状态-空白校准");
		map.put("9", "分析仪(状态)-工作状态-标样校准");
		map.put("10", "分析仪(状态)-工作状态-初始化");
		map.put("11", "分析仪(状态)-工作状态-停止测试");
		equipmentMap.put("i12001", map);
		map = new HashMap<String, String>();//分析仪与数采仪通讯状态
		map.put("0", "分析仪(状态)-分析仪与数采仪通讯-正常");
		map.put("1", "分析仪(状态)-分析仪与数采仪通讯-异常");
		equipmentMap.put("i12002", map);
		equipmentMap.put("i12003", "分析仪(状态)-反应试剂余量");
		map = new HashMap<String, String>();//分析仪报警状态
		map.put("0", "分析仪(状态)-分析仪报警状态-无告警");
		map.put("1", "分析仪(状态)-分析仪报警状态-缺试剂告警");
		map.put("2", "分析仪(状态)-分析仪报警状态-缺水样告警");
		map.put("3", "分析仪(状态)-分析仪报警状态-缺蒸馏水告警");
		map.put("4", "分析仪(状态)-分析仪报警状态-缺标液告警");
		map.put("5", "分析仪(状态)-分析仪报警状态-仪表漏液告警");
		map.put("6", "分析仪(状态)-分析仪报警状态-标定异常告警");
		map.put("7", "分析仪(状态)-分析仪报警状态-超量程告警");
		map.put("8", "分析仪(状态)-分析仪报警状态-加热异常");
		map.put("9", "分析仪(状态)-分析仪报警状态-低试剂预警");
		map.put("10", "分析仪(状态)-分析仪报警状态-超上限告警");
		map.put("11", "分析仪(状态)-分析仪报警状态-超下限告警");
		map.put("12", "分析仪(状态)-分析仪报警状态-仪表内部其它异常");
		map.put("13", "分析仪(状态)-分析仪报警状态-滴定异常告警");
		map.put("14", "分析仪(状态)-分析仪报警状态-电极异常告警");
		map.put("15", "分析仪(状态)-分析仪报警状态-量程切换告警");
		map.put("16", "分析仪(状态)-分析仪报警状态-参数设置告警");
		map.put("17", "分析仪(状态)-分析仪报警状态-PH电极电位异常");
		map.put("18", "分析仪(状态)-分析仪报警状态-电导率电极异常");
		map.put("19", "分析仪(状态)-分析仪报警状态-浊度光度异常");
		map.put("20", "分析仪(状态)-分析仪报警状态-溶解氧电极异常");
		map.put("21", "分析仪(状态)-分析仪报警状态-溶解氧光强异常");
		equipmentMap.put("i12031", map);
		map = new HashMap<String, String>();//工控机工作状态
		map.put("0", "工控机(状态)-工作状态-运行");
		map.put("1", "工控机(状态)-工作状态-停机");
		map.put("2", "工控机(状态)-工作状态-故障");
		map.put("3", "工控机(状态)-工作状态-维护");
		equipmentMap.put("i22001", map);
		map = new HashMap<String, String>();//工控机用户状态
		map.put("0", "工控机(状态)-用户状态-普通用户");
		map.put("1", "工控机(状态)-用户状态-管理员");
		map.put("2", "工控机(状态)-用户状态-维护人员");
		equipmentMap.put("i22002", map);
		map = new HashMap<String, String>();//工控机与上位机通讯状态
		map.put("0", "工控机(状态)-工控机与上位机通讯状态-正常");
		map.put("1", "工控机(状态)-工控机与上位机通讯状态-异常");
		equipmentMap.put("i22003", map);
		equipmentMap.put("i13001", "分析仪(参数)-测量量程");
		equipmentMap.put("i13002", "分析仪(参数)-测量精度");
		equipmentMap.put("i13003", "分析仪(参数)-测量间隔");
		equipmentMap.put("i13004", "分析仪(参数)-消解温度");
		equipmentMap.put("i13005", "分析仪(参数)-消解时长");
		equipmentMap.put("i13006", "分析仪(参数)-空白校准时间");
		equipmentMap.put("i13007", "分析仪(参数)-曲线截距");
		equipmentMap.put("i13008", "分析仪(参数)-曲线斜率");
		equipmentMap.put("i13009", "分析仪(参数)-测量检出限");
		equipmentMap.put("i13010", "分析仪(参数)-测量信号值");
		equipmentMap.put("i13011", "分析仪(参数)-线性相关系数");
		equipmentMap.put("i13012", "分析仪(参数)-二次多项式系数");
		equipmentMap.put("i13013", "分析仪(参数)-标准样校准时间");
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void doFilter(ChannelHandlerContext ctx,WaterInstructionsDto dto,
			Water2018InstructionsChain chain,RedisUtil redisUtil,IBackControlLogRepository repository, boolean interrupt) {
		if(Water2018InstructionsTypeEnum.water2018_3020.getCn().equals(dto.getCn())) {
			interrupt = true;
			try {
				String dataTime = dto.getDataTime();
				String iflag = Integer.toBinaryString(dto.getFlag());
				String isAnswer = iflag.substring(iflag.length()-1);
				String mn = dto.getMn();
				Timestamp timestamp = Water2018InstructionsHandleUtil.getTimestamp(dataTime);
				if(StringUtils.isEmpty(dataTime)) {
					
				}else {
					String[] factorDatas = dto.getFactorData().split(";");
					for(String datas:factorDatas) {
						datas = datas+",";
						String PolId = Water2018InstructionsHandleUtil.getInstructions_PolId(datas);
						Map<String,String> dataMap = Water2018InstructionsHandleUtil.getInstructions_EquipmentInfo(datas);
						for(String key:dataMap.keySet()) {
							EquipmentInfo info = new EquipmentInfo();
							info.setMn(mn);
							info.setFactorCode(PolId);
							info.setCode(key);
							info.setDataTime(timestamp);
							info.setInfo(dataMap.get(key));
							Object obj = equipmentMap.get(key);
							if(obj instanceof String) {
								info.setExplain(obj.toString());
							}else {
								info.setExplain(((HashMap<String, String>)obj).get(dataMap.get(key)));
							}
							redisUtil.lSet("equipmentInfo", info);
						}
						
					}
				}
				if("1".equals(isAnswer)) {
					System.out.println("正在回复设备信息（日志，状态，参数）！！！！！！！！！！！！！");
					String qn = dto.getQn();
					String pw = dto.getPw();
					String datas = "QN="+qn+";ST=91;CN=9014;PW="+pw+";MN="+mn+";Flag=8;CP=&&&&";
					int dataLength = datas.length();//数据段长度
					String crc = CRCCheck.crc16(datas, dataLength);
					String dataLengthString = Water2018InstructionsHandleUtil.getDataLengthString(dataLength+"");
					String returnInfo = "##"+dataLengthString+datas+crc+"\r\n";
					ctx.channel().writeAndFlush(returnInfo);
				}
			} catch (Exception e) {
				System.out.println("设备信息（日志，状态，参数）解析错误：");
			}
		}
		chain.doFilter(ctx,dto,chain,redisUtil,repository,interrupt);
	}
	
}
